﻿using ShiQuan.IotDevice.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using ShiQuan.Utility;
using ShiQuan.WebControl;

namespace ShiQuan.IotDevice.Business
{
    /// <summary>
    /// 设备信息业务处理
    /// </summary>
    public class IotDeviceInfoBLL:BaseIotDeviceBLL<IotDeviceInfoEntity>
    {
        /// <summary>
        /// 获取设备类型列表
        /// </summary>
        /// <returns></returns>
        public List<NameValueModel> GetTypeList()
        {
            var data = new List<NameValueModel>();
            data.Add(new NameValueModel() { Name= "双温度传感器设备", Value= "two_temperature" });
            data.Add(new NameValueModel() { Name = "温湿度传感器设备", Value = "humiture" });
            return data;
        }
        /// <summary>
        /// 获取树状列表
        /// </summary>
        /// <param name="queryJson"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public List<TreeModel> GetTypeTree(bool root = true)
        {
            var data = this.GetTypeList();

            var treeList = new List<TreeModel>();
            TreeModel rootModel = null;
            if (root)
            {
                rootModel = new TreeModel();
                bool hasChildren = data.Count() == 0 ? false : true;
                rootModel.id = "-1";
                rootModel.text = "所有类型";
                rootModel.value = "-1";
                rootModel.isexpand = true;
                rootModel.complete = true;
                rootModel.hasChildren = hasChildren;
                rootModel.parentId = "0";
                //tree.icon = item.Icon;
                treeList.Add(rootModel);
            }
            foreach (NameValueModel item in data)
            {
                TreeModel tree = new TreeModel();
                tree.id = item.Value;
                tree.text = item.Name;
                tree.value = item.Value;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = false;
                if (rootModel != null)
                    tree.parentId = rootModel.id;
                else
                    tree.parentId = "0";
                //tree.icon = item.Icon;
                treeList.Add(tree);
            }
            return treeList;
        }
        /// <summary>
        /// 获取树状列表
        /// </summary>
        /// <param name="queryJson"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        public List<TreeModel> GetTreeList(string queryJson, bool root = true)
        {
            var data = this.GetList(queryJson).OrderBy(my=>my.Code).ToList();
            var treeList = new List<TreeModel>();
            TreeModel rootModel = null;
            if (root)
            {
                rootModel = new TreeModel();
                bool hasChildren = data.Count() == 0 ? false : true;
                rootModel.id = "-1";
                rootModel.text = "所有设备";
                rootModel.value = "-1";
                rootModel.isexpand = true;
                rootModel.complete = true;
                rootModel.hasChildren = hasChildren;
                rootModel.parentId = "0";
                //tree.icon = item.Icon;
                treeList.Add(rootModel);
            }
            foreach (IotDeviceInfoEntity item in data)
            {
                TreeModel tree = new TreeModel();
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = false;
                if (rootModel != null)
                    tree.parentId = rootModel.id;
                else
                    tree.parentId = "0";
                //tree.icon = item.Icon;
                treeList.Add(tree);
            }
            return treeList;
        }
        /// <summary>
        /// 查询过滤
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        protected override Expression<Func<IotDeviceInfoEntity, bool>> QueryStringToExpression(Expression<Func<IotDeviceInfoEntity, bool>> expression, string queryJson)
        {
            if (string.IsNullOrEmpty(queryJson))
                return expression;
            var queryParam = queryJson.ToJObject();
            if(queryParam["TypeId"].IsEmpty() == false && queryParam["TypeId"].ToString() != "-1")
            {
                string keyword = queryParam["TypeId"].ToString();
                expression = expression.And(my => my.TypeId == keyword);
            }
            if (!queryParam["condition"].IsEmpty() && !queryParam["keyword"].IsEmpty())
            {
                string condition = queryParam["condition"].ToString();
                string keyword = queryParam["keyword"].ToString();
                switch (condition)
                {
                    default: break;
                }
            }
            return expression;
        }
    }
}
